package com.shopping.controller;

import com.shopping.bean.OrderProduct;
import com.shopping.bean.Product;
import com.shopping.service.ICategoryService;
import com.shopping.service.IProductService;
import com.shopping.util.LayData;
import com.shopping.util.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Controller
@RequestMapping("product")
public class ProductController {
    @Autowired
    IProductService productService;

    @Autowired
    ICategoryService categoryService;
    @RequestMapping("list")
    @ResponseBody
    public LayData getList(Page page, String text, @RequestParam(required = false) String category) {
        List<Product> list = productService.getList(page, text, category);
        int count = productService.getCount(text, category);
        return LayData.success(count, list);
    }

    @RequestMapping("delete")
    @ResponseBody
    public int delete(int id){
        return productService.deleteProduct(id);
    }
    @RequestMapping("getChildren")
    @ResponseBody
    public List<Product> getChildren(int id){
        return productService.getChildren(id);
    }
    @RequestMapping("add")
    @ResponseBody
    public int add(Product product){
        return productService.addProduct(product);
    }
    @RequestMapping("edit")
    @ResponseBody
    public  int edit(Product product){
        return productService.editProduct(product);
    }

    //根据分类名称查询产品列表
    @RequestMapping("listByCategory")
    @ResponseBody
    public LayData getProductsByCategoryName(@RequestParam("categoryname") String categoryname) {
        List<Product> list = productService.getProductsByCategoryName(categoryname);
        return LayData.success(list.size(), list);
    }
    @RequestMapping("getproduct")
    @ResponseBody
    public LayData getList(Integer id){
        Product product=productService.getProductByID(id);
        return LayData.success(product);
    }

    @RequestMapping("updateStockAndSales")
    @ResponseBody
    public LayData updateStockAndSales(@RequestBody List<OrderProduct> orderProducts) {
        try {
            productService.updateStockAndSales(orderProducts);
            return LayData.success(200,"商品库存和销量更新成功");
        } catch (Exception e) {
            return LayData.success("商品库存和销量更新失败: " + e.getMessage());
        }
    }

    @RequestMapping("salesAndStocks")
    @ResponseBody
    public LayData getSalesAndStocks() {
        try {
            List<Product> products = productService.getAllProductSalesAndStocks();
            // 处理商品类型为 null 的情况
            products.forEach(product -> {
                if (product.getProductcategory() == null) {
                    product.setProductcategory("未分类");
                }
                if (product.getProductcategory2() == null) {
                    product.setProductcategory2("未分类");
                }
            });

            Map<String, Object> data = new HashMap<>();
            data.put("products", products);
            return LayData.success(200, "", data);
        } catch (Exception e) {
            return LayData.success("获取商品销量和库存数据失败: " + e.getMessage());
        }
    }

}
